package com.neusoft.po;

/**
 * 钱包实体类，表示用户的虚拟钱包，包含用户ID和钱包余额。
 */
public class Wallet {
    private String userId; // 用户ID，唯一标识一个用户
    private Double balance; // 钱包余额，表示用户的当前资金
    private Double frozenBalance; // 冻结余额，用于存放未确认的转账金额
    private Double overdraftLimit; // 透支额度
    private Double debtAmount; // 当前负债金额

    // Constructors

    /**
     * 默认构造函数，初始化一个空的钱包对象。
     */
    public Wallet() {}

    /**
     * 带参数的构造函数，用于创建包含指定用户ID和余额的钱包对象。
     * 
     * @param userId 用户ID
     * @param balance 初始余额
     * @param frozenBalance 初始冻结余额
     * @param overdraftLimit 透支额度
     * @param debtAmount 负债金额
     */
    public Wallet(String userId, Double balance, Double frozenBalance, Double overdraftLimit, Double debtAmount) {
        this.userId = userId;
        this.balance = balance;
        this.frozenBalance = frozenBalance;
        this.overdraftLimit = overdraftLimit;
        this.debtAmount = debtAmount;
    }

    // Getters and Setters

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public Double getBalance() {
        return balance;
    }

    public void setBalance(Double balance) {
        this.balance = balance;
    }

    public Double getFrozenBalance() {
        return frozenBalance;
    }

    public void setFrozenBalance(Double frozenBalance) {
        this.frozenBalance = frozenBalance;
    }

    public Double getOverdraftLimit() {
        return overdraftLimit;
    }

    public void setOverdraftLimit(Double overdraftLimit) {
        this.overdraftLimit = overdraftLimit;
    }

    public Double getDebtAmount() {
        return debtAmount;
    }

    public void setDebtAmount(Double debtAmount) {
        this.debtAmount = debtAmount;
    }

    // 行为方法

    // 充值方法
    public void recharge(Double amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException("充值金额必须大于零");
        }

        if(this.debtAmount != 0){
            if(debtAmount <= amount)
            {
                debtAmount = 0.0;
                amount -= debtAmount;
            }
            else{
                debtAmount -= amount;
                amount = 0.0;
            }
        }
        this.balance += amount;
    }

    // 扣款方法
    public boolean withdraw(Double amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException("扣款金额必须大于零");
        }
        if (this.balance >= amount) {
            this.balance -= amount;
            return true;
        } 
        else{
            throw new IllegalArgumentException("余额不足");
        }
    }

    // 冻结资金的方法
    public void freezeFunds(Double amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException("冻结金额必须大于零");
        }
        if (this.balance >= amount) {
            this.balance -= amount;
            this.frozenBalance += amount;
        } else {
            throw new IllegalArgumentException("余额不足以冻结");
        }
    }

}
